package hd.util.widget;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import hd.util.ScreenHelper;

/**
 * Pager Tab View.PageView的TabView
 * <p/>
 * Created by Haidy on 15/3/9.
 */
public class PagerTabView extends ViewGroup {

    private LinearLayout scrollBarView;
    private OnCheckedChangeListener onCheckedChangeListener;

    public PagerTabView(Context context) {
        this(context, null);
    }

    public PagerTabView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PagerTabView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        initView();
    }

    /**
     * 初始化界面
     */
    private void initView() {
        scrollBarView = new LinearLayout(getContext());
        View view = new View(getContext());
        view.setBackgroundColor(Color.rgb(77, 156, 255));
        view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        scrollBarView.addView(view);
        addView(scrollBarView);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        float itemWidth = getMeasuredWidth() / (getChildCount() - 1);
        int height = getMeasuredHeight();
        int start = 0;
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            if (child != scrollBarView) {
                child.layout(start, 0, (int) (start + itemWidth), height);
                start += itemWidth;
            }
        }
        scrollBarView.layout(0, getMeasuredHeight() - ScreenHelper.dp2px(getContext(), 2), getMeasuredWidth(), getMeasuredHeight());
    }

    public void scrollBarScrollTo(int offsetPixels) {
        scrollBarView.scrollTo(offsetPixels, 0);
        scrollBarView.invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(width, height);
        float itemWidth = getMeasuredWidth() / (getChildCount() - 1);
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            if (child != scrollBarView) {
                child.measure(MeasureSpec.makeMeasureSpec((int) itemWidth, MeasureSpec.getMode(widthMeasureSpec)), heightMeasureSpec);
            }
        }
        scrollBarView.measure(MeasureSpec.makeMeasureSpec((int) itemWidth, MeasureSpec.getMode(widthMeasureSpec)), MeasureSpec.makeMeasureSpec(ScreenHelper.dp2px(getContext(), 2), MeasureSpec.getMode(heightMeasureSpec)));
    }

    /**
     * 设置选择的监听器
     *
     * @param onCheckedChangeListener 回调接口
     */
    public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
        this.onCheckedChangeListener = onCheckedChangeListener;
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            if (child != scrollBarView) {
                child.setOnClickListener(listener);
            }
        }
    }

    private OnClickListener listener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (onCheckedChangeListener != null) {
                onCheckedChangeListener.onChecked(v);
            }
        }
    };

    public interface OnCheckedChangeListener {
        void onChecked(View view);
    }
}
